﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;
using DevExpress.XtraGrid.Views.Grid;

namespace QLTV.Forms
{
    public partial class frmBanLamViec : BaseForm
    {
        public frmBanLamViec()
        {
            InitializeComponent();
            statusDataBar1.addTextChange(viewLich);
            statusDataBar1.AddControlPrint(LịchHẹnThuyềnViên);
            statusDataBar2.AddControlPrint(DanhSachThuyenVien);

        }

        private void frmBanLamViec_Load(object sender, EventArgs e)
        {
            Database.dsChartTableAdapters.tadTong tong = new Database.dsChartTableAdapters.tadTong();
            tong.Fill(dsChart.TongSo);
            layoutThongKe.Text = String.Format("Thống kê: {0} (thuyền viên)", dsChart.TongSo.Rows[0][0]);
            RunLongThread(loadData);

        }
        private void loadData()
        {
         
            // TODO: This line of code loads data into the 'dsChart.tblMarital' table. You can move, or remove it, as needed.
            this.tblMaritalTableAdapter.Fill(this.dsChart.tblMarital);
            // TODO: This line of code loads data into the 'dsChart.tblAge' table. You can move, or remove it, as needed.
            this.tblAgeTableAdapter.Fill(this.dsChart.tblAge);
            // TODO: This line of code loads data into the 'dsChart.tblAge' table. You can move, or remove it, as needed.
            this.tblAgeTableAdapter.Fill(this.dsChart.tblAge);
            // TODO: This line of code loads data into the 'dsChart.tblRank' table. You can move, or remove it, as needed.
            this.tblRankTableAdapter.Fill(this.dsChart.tblRank);
            // TODO: This line of code loads data into the 'dsTongQuan.tblLichHen' table. You can move, or remove it, as needed.
            this.tblLichHenTableAdapter.Fill(this.dsTongQuan.tblLichHen);
            // TODO: This line of code loads data into the 'dsCrew.tblViewCrew' table. You can move, or remove it, as needed.
            this.tblViewCrewTableAdapter.Fill(this.dsCrew.tblViewCrew);
            // TODO: This line of code loads data into the 'dsCrew.tblCrew' table. You can move, or remove it, as needed.
            // TODO: This line of code loads data into the 'dsChart.viewStatus' table. You can move, or remove it, as needed.
            this.viewStatusTableAdapter.Fill(this.dsChart.viewStatus);
            // TODO: This line of code loads data into the 'dsChart.tblEducation' table. You can move, or remove it, as needed.
            this.tblEducationTableAdapter.Fill(this.dsChart.tblEducation);
            // TODO: This line of code loads data into the 'dsDiaChi.tblTinhThanh' table. You can move, or remove it, as needed.
            this.tblTinhThanhTableAdapter.Fill(this.dsDiaChi.tblTinhThanh);
            // TODO: This line of code loads data into the 'dsDiaChi.tblQuanHuyen' table. You can move, or remove it, as needed.
            this.tblQuanHuyenTableAdapter.Fill(this.dsDiaChi.tblQuanHuyen);
            // TODO: This line of code loads data into the 'dsDiaChi.tblPhuongXa' table. You can move, or remove it, as needed.
            this.tblPhuongXaTableAdapter.Fill(this.dsDiaChi.tblPhuongXa);
            _wait.ShouldCloseNow = true;
        }

        private void chartControl1_Click(object sender, EventArgs e)
        {

        }

        private void gridCrew_Click(object sender, EventArgs e)
        {

        }

        private void statusDataBar1_UpdateClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            tblLichHenBindingSource.EndEdit();
            tblLichHenTableAdapter.Update(dsTongQuan.tblLichHen);
            alertControl1.Show(this, rm.GetString("sMessageCaption", myCultureInfo), rm.GetString("sMessageSuccess", myCultureInfo));
          //  dsTongQuan.AcceptChanges();
        }

        private void statusDataBar1_NewClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            tblLichHenBindingSource.AddNew();
        }

        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            // if (viewCrew.FocusedRowHandle > -1)
            // {
  
            //}


        }

        private void viewLich_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            viewLich.SetRowCellValue(e.RowHandle, colNgườilậpkếhoạch, Program.EmployeeName);

        }

        private void viewCrew_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column == colSTT)
            {
                e.CellValue = Convert.ToString(e.RowHandle + 1);
            }

            GridView currentView = sender as GridView;
            if (e.RowHandle == currentView.FocusedRowHandle) return;
            if (e.RowHandle > -1)
            {
                string status = currentView.GetRowCellValue(e.RowHandle, currentView.Columns["cStatus"]).ToString().ToUpper();
                switch (status)
                {
                    case "NEW CREW":
                        if (e.Column == colNgayKyHD)
                        {
                            e.Appearance.BackColor = Color.Yellow;
                            e.Appearance.ForeColor = Color.Black;

                        }
                        break;

                    case "ON RESIGN":
                        if (e.Column == colNgayCatHD)
                        {
                            e.Appearance.BackColor = Color.Red;
                            e.Appearance.ForeColor = Color.White;

                        }
                        break;
                    case "ON RETIRE":
                        if (e.Column == colNgayNghiHuu)
                        {
                            e.Appearance.BackColor = Color.Red;
                            e.Appearance.ForeColor = Color.White;

                        }

                        break;
                    case "ON VACATION":
                        if (e.Column == colNgaySS || e.Column == colRoiTau || e.Column == colThoiGianYcNghi)
                        {
                            e.Appearance.BackColor = Color.Yellow;
                            e.Appearance.ForeColor = Color.Black;

                        }

                        break;
                    case "ON BOARD":
                        if (e.Column == colTenTau || e.Column == colEmbarked || e.Column == colDuKienRoiTau)
                        {
                            e.Appearance.BackColor = Color.Yellow;
                            e.Appearance.ForeColor = Color.Black;

                        }

                        break;
                    default:
                        break;
                }

            }
        }


        private void statusDataBar1_DeleteClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            if (viewLich.FocusedRowHandle > -1)
            {
                if (XtraMessageBox.Show(rm.GetString("sConfirm", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    tblLichHenBindingSource.RemoveCurrent();
                    tblLichHenBindingSource.EndEdit();
                    tblLichHenTableAdapter.Update(dsTongQuan.tblLichHen);
                    alertControl1.Show(this, rm.GetString("sMessageCaption", myCultureInfo), rm.GetString("sMessageSuccess", myCultureInfo));
                }

            }
        }

        private void viewLich_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            //GridView currentView = sender as GridView;
            //if (e.RowHandle == currentView.FocusedRowHandle) return;
            //if (e.RowHandle > -1)
            //{
            //    bool isDone = (bool)currentView.GetRowCellValue(e.RowHandle, currentView.Columns["[Đã giải quyết]"]);
            //    if(isDone)


            //}
        }

        private void radioGroup1_EditValueChanged(object sender, EventArgs e)
        {
            if (!splitter.Collapsed)
                splitter.Collapsed = true;
            switch (radioGroup1.EditValue.ToString())
            {
                case "allin":
                    tblCrewBindingSource.Filter = "cStatus='On Board' or cStatus='On Vacation' or cStatus='New Crew'";
                    break;
                case "onboard":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Board'";
                    break;
                case "onretire":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Retire'";
                    break;
                case "onvacation":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Vacation'";
                    break;
                case "onresign":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Resign'";
                    break;
                case "newcrew":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='New Crew'";
                    break;
                case "left":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Resign' or cStatus='On Retire'";
                    break;
            }

        }

        private void statusDataBar1_UndoClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            tblLichHenBindingSource.CancelEdit();
        }

        private void frmBanLamViec_Resize(object sender, EventArgs e)
        {
            splitter.SplitterPosition = splitter.Size.Height - 60;
        }

        private void viewLich_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {

        }

        private void viewLich_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            viewLich.SetRowCellValue(e.RowHandle, colNgườilậpkếhoạch, Program.EmployeeName);
        }

        private void statusDataBar1_LoadClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblLichHenTableAdapter.Fill(this.dsTongQuan.tblLichHen);
        }

        private void statusDataBar2_LoadClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblViewCrewTableAdapter.Fill(this.dsCrew.tblViewCrew);
        }

        private void dathen_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            string rank = viewCrew.GetFocusedRowCellValue("cRankName").ToString();
            string CODE = viewCrew.GetFocusedRowCellValue(colnCrewId).ToString();
            string name = String.Format("{0} {1} {2}", viewCrew.GetFocusedRowCellValue("cFamilyName"), viewCrew.GetFocusedRowCellValue("cMiddleName"), viewCrew.GetFocusedRowCellValue("cFirstName"));
            KhaiBao.frmLichHen f = new KhaiBao.frmLichHen(CODE, rank, name);
            f.ShowDialog();
            tblLichHenBindingSource.DataSource = tblLichHenTableAdapter.GetData();
        }

        private void viewCrew_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column == colSTT && e.RowHandle > -1)
            {
                e.DisplayText = Convert.ToString(e.RowHandle + 1);
            }
        }

        private void viewLich_CalcPreviewText(object sender, CalcPreviewTextEventArgs e)
        {
            e.PreviewText = "Nội dung: " + e.PreviewText;
        }
    }
}